var gw_num = 0;//展开表格的个数
var gw = {
    //当前打开的行
    expandIndex:-1,
    //新增状态
    formStatus:false,
    //判断按钮显示
    setBtnStatus:function () {
        var row = $('#grid_gw').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_gw_del").show();
        } else {
            $("#btn_gw_del").hide();
        }
    },
    //重置状态
    resetStatus:function () {
        gw.setBtnStatus();
        gw.expandIndex = -1;
        gw.formStatus = false;
    }
}

$(function () {
    //表格初始加载
    $('#grid_gw').datagrid({
        view:detailview,
        detailFormatter:function () {
            return '<div class="detail"></div>';
        },
        onExpandRow:function (index, row) {
            $("#layout_gw").mask();
            //关闭展开的行
            if (gw.expandIndex != -1) {
                $('#grid_gw').datagrid("collapseRow", gw.expandIndex);
            }
            //记录当前展开哪行
            gw.expandIndex = index;
            show_gw_form(index, row);//加载表格面板
        },
        onCollapseRow:function () {
            //删除当前记录的行
            gw.expandIndex = -1;
        },
        onLoadSuccess:function(){
            gw.setBtnStatus();
        },
        onClickRow:function(){
            gw.setBtnStatus();
        },
        onSelect:function(){
            gw.setBtnStatus();
        },
        onDblClickCell:function (index, field) {
            //双击事件
            if (field == "mc") {
                $('#grid_gw').datagrid("expandRow", index);
            }
        }
    });

    //*************按钮触发事件***********
    //新增触发
    $('#btn_gw_add').click(
        gw_add
    );
    //删除触发
    $('#btn_gw_del').click(
        gw_delete
    );
    //搜索触发
    $('#btn_gw_search').click(
        gw_search
    );
});

//新增岗位
function gw_add() {
    //关闭打开的行
    if (gw.expandIndex != -1) {
        $('#grid_gw').datagrid("collapseRow", gw.expandIndex);
    }
    //已有新增判断
    if (!gw.formStatus) {
        $('#grid_gw').datagrid('insertRow', {index:0, row:{isNewRecord:true}});
        gw.formStatus = true;
    }
    $('#grid_gw').datagrid('expandRow', 0);
    $('#grid_gw').datagrid('selectRow', 0);
}

// 保存岗位表单
function gw_save(index) {
    $("#layout_gw").mask();
    $('#grid_gw').datagrid('getRowDetail', index).find('form').form('submit', {
        url:$.webPath() + '/sys/gw/add_gw',
        onSubmit:function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                //$.messager.progress('close');
                $("#layout_gw").mask('hide');
                return false;
            }
            return isValid;
        },
        success:function (data) {
            $("#layout_gw").mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == 200) {
                $('#grid_gw').datagrid('reload');
                gw.resetStatus();
                $.messager.show({
                    title:'提示',
                    msg:obj.message,
                    timeout:3000,
                    showType:'slide'
                });
            } else{
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

//取消岗位表单
function gw_cancel(index) {
    var row = $('#grid_gw').datagrid('getRows')[index];
    if (row.isNewRecord) {
        $('#grid_gw').datagrid('deleteRow', index);
    } else {
        $('#grid_gw').datagrid('collapseRow', index);
    }
    $('#grid_gw').datagrid('reload');
    gw.resetStatus();
}

//删除岗位
function gw_delete() {
    var row = $('#grid_gw').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该岗位？', function (r) {
            if (r) {
                var index = $('#grid_gw').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/sys/gw/delete_gw",
                    dataType: 'json',
                    type: 'POST',
                    data: {gwId:row.id},
                    beforeSend: function () {
                        $("#layout_gw").mask();
                    },
                    success:function(data){
                        $("#layout_gw").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_gw').datagrid('deleteRow', index);
                            $('#grid_gw').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                            gw.resetStatus();
                        }else{
                            $.messager.alert('提示', data.message, 'error');
                        }
                    }
                });
//                $.post($.webPath() + '/sys/gw/delete_gw', {gwId:row.id}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.statusCode == 200){
//                        $('#grid_gw').datagrid('deleteRow', index);
//                        $.messager.show({
//                            title:'提示',
//                            msg:obj.message,
//                            timeout:3000,
//                            showType:'slide'
//                        });
//                        gw.resetStatus();
//                    }else{
//                        $.messager.alert('提示', obj.message, 'error');
//                    }
//                });
            }
        });
    } else {
        $.messager.alert('提示', '请选择要删除的岗位！', 'warning');
    }
}

//查询岗位
function gw_search() {
    $('#grid_gw').datagrid('load', {
        mc:$('#gw_mc').val()
    });
}

//展开岗位表单
function show_gw_form(index, row) {
    $("#layout_gw").mask("hide");
    var ddv = $("#grid_gw").datagrid('getRowDetail', index).find('div.detail');
    var url = $.webPath() + '/sys/gw/gw_content?index=' + index;
    if (row.id)
        url += '&gwId=' + row.id;
    ddv.panel({
        border:false,
        cache:true,
        href:url,
        onLoad:function () {
            $('#grid_gw').datagrid('fixDetailRowHeight', index);
            $('#grid_gw').datagrid('selectRow', index);
            if (row.jsList_id != null){
                $('#grid_gw').datagrid('getRowDetail', index).find('input[comboname="gw.jsList.id"]').combobox('setValues', row.jsList_id.split(","));
            }
            gw.setBtnStatus();
        }
    });
    $('#grid_gw').datagrid('fixDetailRowHeight', index);
    gw_num++;
}